{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.session import Environment, GsSession\n",
    "\n",
    "client_id = None # Supply your application id\n",
    "client_secret = None # Supply your client secret\n",
    "\n",
    "GsSession.use(Environment.PROD, client_id, client_secret, ('run_analytics',))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Create a dummy portfolio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.instrument import IRSwap, IRSwaption\n",
    "from gs_quant.markets.portfolio import Portfolio\n",
    "\n",
    "swap = IRSwap(notional_currency='EUR', termination_date='10y', pay_or_receive='Pay')\n",
    "swaption = IRSwaption(notional_currency='EUR', termination_date='10y', expiration_date='1y', pay_or_receive='Receive')\n",
    "\n",
    "portfolio = Portfolio((swap, swaption))\n",
    "portfolio.resolve()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Calculate PnlExplain from 1 week ago (5 business days) to today, compare with the dollar price difference and examine the breakdown"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.datetime.date import business_day_offset\n",
    "from gs_quant.markets import CloseMarket, PricingContext, close_market_date\n",
    "from gs_quant.risk import DollarPrice, PnlExplain\n",
    "\n",
    "to_date = close_market_date()\n",
    "\n",
    "# 5 business days ago\n",
    "from_date = business_day_offset(to_date, -5)\n",
    "\n",
    "# A risk measure for calculating PnlExplain from that date\n",
    "explain = PnlExplain(CloseMarket(date=to_date))\n",
    "\n",
    "# Calculate PnlExplain and dollar price from 1 week ago\n",
    "with PricingContext(pricing_date=from_date):\n",
    "    result = portfolio.calc((DollarPrice, explain))\n",
    "    \n",
    "# Calculate dollar price with the \"to\" market but \"from\" pricing date\n",
    "with PricingContext(pricing_date=from_date, market=CloseMarket(date=to_date)):\n",
    "    to_market_price = portfolio.dollar_price()\n",
    "\n",
    "# Calculate dollar price with the \"to\" market and pricing date\n",
    "with PricingContext(pricing_date=to_date):\n",
    "    to_price = portfolio.dollar_price()\n",
    "\n",
    "# Compute the time component (PnlExplain does not do this)\n",
    "time_value = to_price.aggregate() - to_market_price.aggregate()\n",
    "    \n",
    "print(f'Dollar price difference: {to_price.aggregate() - result[DollarPrice].aggregate():.0f}')\n",
    "print(f'Pnl explain + time value total: {result[explain].aggregate().value.sum() + time_value:.0f}')\n",
    "print(f'Pnl explain total: {result[explain].aggregate().value.sum():.0f}')\n",
    "print(f'Time value total: {time_value:.0f}')\n",
    "\n",
    "# Show the PnlExplain breakdown\n",
    "explain_all = result[explain].aggregate()\n",
    "explain_all[explain_all.value.abs() > 1.0].round(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
